package com.wayhome.srpingbootmybatis.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.wayhome.srpingbootmybatis.dto.OpspRegDTO;
import com.wayhome.srpingbootmybatis.excel.PsnNoExcel;
import com.wayhome.srpingbootmybatis.mapper.OpspRegMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class OpspRegListener extends AnalysisEventListener<PsnNoExcel> {

    List<String> delList = new ArrayList<>();
    List<String> manbing = Arrays.asList("M00100", "M00200" , "M00401" , "M00500" ,"M00539" , "M00700" , "M00901" , "M00902" ,
            "M00903" ,"M00914" ,"M01101" ,"M01200" ,"M01301" , "M01502" ,"M01600" ,"M01701" ,"M01702" ,"M01704" ,"M01904" ,
            "M01906" ,"M01907" ,"M01910" ,"M01913" ,"M01917" ,"M02109" ,"M02208" ,"M02300" ,"M02400" ,"M02500" ,"M02700" ,
            "M02900" ,"M03104" ,"M03200" ,"M03600" ,"M03802" ,"M03900" ,"M04100" ,"M04200" ,"M04301" ,"M04401" ,"M04600" ,
            "M04802" ,"M04806" ,"M04817" ,"M04905" ,"M05200" , "M05300" ,"M05400" ,"M05600" ,"M05700" ,"M05800" ,"M05901" ,
            "M06200" ,"M06400" ,"M06501" ,"M06700" ,"M06800" , "M06900" ,"M07000" ,"M07101" ,"M07103" ,"M07104" ,"M07106" ,
            "M07107" ,"M07200" ,"M07300" , "M07401" ,"M07600" ,"M07700" ,"M07800" ,"M08501" ,"M08800" ,"M09102" ,"M09106" ,
            "M09800" ,"M09805" ,"M09806" ,"M10700" ,"M10800" ,"M11801");

    public List<String> getDelList() {
        return delList;
    }

    private OpspRegMapper opspRegMapper;

    public OpspRegListener(OpspRegMapper opspRegMapper) {
        this.opspRegMapper = opspRegMapper;
    }

    List<String> psnNoList = new ArrayList<>();

    @Override
    public void invoke(PsnNoExcel psnNoExcel, AnalysisContext analysisContext) {
        String psnNo = psnNoExcel.getPsnNo();
        psnNo = psnNo.substring(1);
        psnNoList.add(psnNo);
        List<OpspRegDTO> opspRegDTOS = new ArrayList<>();
        if (psnNoList.size() == 100) {
            if (psnNoList.contains("1399001275703") || psnNoList.contains("139900142920")) {
                System.out.println("应该有数据");
            }
            opspRegDTOS = opspRegMapper.queryOpspRegListByPsnNo(psnNoList);
            psnNoList.clear();
        }
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format = now.format(formatter);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        if (!CollectionUtils.isEmpty(opspRegDTOS)) {
            opspRegDTOS = opspRegDTOS.stream().filter(item-> manbing.contains(item.getOpspDiseCode())).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(opspRegDTOS)) {
                for (OpspRegDTO opspRegDTO : opspRegDTOS) {
                    log.info("通过身份证号码查询到认定慢病记录：{}", JSONObject.toJSONString(opspRegDTO));
                    String begnDateStr = simpleDateFormat.format(opspRegDTO.getBegnDate());
                    String delStr = "update setlcent_db.opsp_reg_d set VALI_FLAG = '0', memo = '因政策原因, 于" +
                            format + "将省本级退休公务员（或10%补充险）慢性病转为失效'" +
                            "where TRT_DCLA_DETL_SN = '" + opspRegDTO.getTrtDclaDetlSn() + "'" +
                            "and PSN_NO = '" + opspRegDTO.getPsnNo() + "'" +
                            "and OPSP_DISE_CODE = '" + opspRegDTO.getOpspDiseCode() + "'" +
                            "and BEGNDATE = '" + begnDateStr +"';";
                    System.out.println(begnDateStr);
                    delList.add(delStr);
                }
            }
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        List<OpspRegDTO> opspRegDTOS = new ArrayList<>();
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format = now.format(formatter);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (!CollectionUtils.isEmpty(opspRegDTOS)) {
            for (OpspRegDTO opspRegDTO : opspRegDTOS) {
                log.info("通过身份证号码查询到认定慢病记录：{}", JSONObject.toJSONString(opspRegDTO));
                String begnDateStr = simpleDateFormat.format(opspRegDTO.getBegnDate());
                String delStr = "update setlcent_db.opsp_reg_d set VALI_FLAG = '0', memo = '因政策原因, 于" +
                        format + "将省本级退休公务员（或10%补充险）慢性病转为失效'" +
                        "where TRT_DCLA_DETL_SN = '" + opspRegDTO.getTrtDclaDetlSn() + "'" +
                        "and PSN_NO = '" + opspRegDTO.getPsnNo() + "'" +
                        "and OPSP_DISE_CODE = '" + opspRegDTO.getOpspDiseCode() + "'" +
                        "and BEGNDATE = '" + begnDateStr +"';";
                System.out.println(begnDateStr);
                delList.add(delStr);
            }
        }
        log.info("执行结束,获取结果：{}", delList);
    }
}
